"""
URL configuration for iClouds project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

from django.conf import settings
from django.contrib import admin
from django.urls import path, include, re_path
from django.views.static import serve
from .views import site

DEVELOG = [
    "2024-08-29\n",
    "#181 变更chat字段为提问文本.\n",
    "#31  增加是否公共好友字段\n",
    "#30  变更类型: Mpeg 为 Lipsync\n",
    "2024-08-28\n",
    "移除声音模型后，相关好友改为默认声音模型。\n",
    "2024-08-26\n",
    "创建推广码: 改变推广码名称为ID。\n",
    "验证推广码: 关闭#71, 改为登录时提供推广ID. \n",
    "小程序登录api-#09: 增加推荐人字段及相关逻辑.\n",
    "2024-08-23\n",
    "ai声音模型: 启动移除模型。移除模型后，同步关闭共享。\n",
    "ai虚拟好友: 启用移除好友。移除好友后，同步关闭共享。并移除被添加的关联好友。\n",
    "添加共享好友: 优化显示该请求的发起者和拥有者个人信息。\n",
    "充值修正: 后台功能，管理员修改用户代币余额.\n",
    "我的推广码: 创建用户的推广二维码.\n",
    "验证推广码: 验证新用户登录，并发放推广奖励.\n",
    "2024-08-23\n",
    "<a href='/docs/#rebot-22-create'>[#rebot-22-create]</a> [post] 变更: 本地三方任务错误日志及状态记录。\n",
    "<a href='/docs/#rebot-11-list'>[#rebot-11-list]</a> [get] 变更: 查询条件share字段改为bool值\n",
    "<a href='/docs/#rebot-11-create'>[#rebot-11-create]</a> [post] 变更: 好友属性增加默认形象照片字段 photo\n",
    "<a href='/docs/#rebot-31-create'>[#rebot-31-create]</a> [post] 变更: 优化模板消息订阅任务的执行流程及扣费,暂停执行hedra视频订阅.\n",
    "<a href='/docs/#rebot-672-list'>[#rebot-672-list]</a> [list] 变更: 添加共享好友显示申请者信息.\n",
    "订阅模板: 新增微信消息模板: 收到服务订单通知。测试通过，但前端页面路径无效，不能创建链接。\n",
    "opencv: 基础扣费，及共享分成。为降低服务器硬件资源小号，创建线程锁以保持单用户运行。用户量大可能导致等待队列过长.\n",
    "lipsync: 基础扣费，及共享分成。创建线程锁以保持单用户运行。用户量大可能导致等待队列过长.\n",
    "hedra: 基础扣费，及共享分成。\n",
    "2024-08-22\n",
    "<a href='/docs/#rebot-541-list'>[#rebot-541-list]</a> [get] 新增: 订阅模板评分查询接口。\n",
    "<a href='/docs/#rebot-54-create'>[#rebot-54-post]</a> [post] 变更: 创建订阅模板评分。\n",
    "[Robot] [#521] [get] 新增: 声音模型评分查询接口。\n",
    "[Robot] [#52] [post] 变更: 创建声音模型评分。\n",
    "[Robot] [#19] [*] 停用: 接口停用，功能转移到 #62\n",
    "[Robot] [#20] [*] 停用: 接口停用，功能转移到 #630,#631\n",
    "[Robot] [#62] [post] 变更: 判断模型创建者。扣费金额变更为基础金额加分成金额，添加分成金额用户入账\n",
    "[Robot] [#11] [*] 变更: 序列化器、过滤器、增加共享权限验证字段。\n",
    "[Robot] [#67] [*] 变更: 移除创建共享好友接口。\n",
    "[Robot] [#671] [post] 新增: 增加创建共享好友接口视图。加入权限认证\n",
    "[Robot] [#672] [get] 新增: 我的好友请求列表及同意和拒绝\n",
    "2024-08-21\n",
    "[Robot] [#512] [get,put,del] 新增: 提供共享模型库的查询、编辑和移除.\n",
    "[Robot] [#51] [post] 变更: 增加用户自定义单价设置.\n",
    "[Robot] [#68] [post] 新增: 创建充值修正接口\n",
    "[Robot] [#50] [post] 变更: 名称为'模型训练', 有效期固定为1年\n",
    "[Robot] [#11] [post] 变更: 创建虚拟好友不再新建TTS模型,改为绑定默认模型ID:5\n",
]
DESCRIPTION = "<br>".join(DEVELOG)


urlpatterns = [
    path("", site.as_view()),
    # path("admin/", admin.site.urls),
    path(
        "api/",
        include(("mysqldb.urls", "mysqldb"), namespace="mysqldb"),
        name="全局接口",
    ),
    path(
        "admin/",
        include(("Admin.urls", "Admin"), namespace="Admin"),
        name="管理员接口",
    ),
    # path(
    #     "site/",
    #     include(("SiteManager.urls", "SiteManager"), namespace="SiteManager"),
    #     name="站点接口",
    # ),
    # path(
    #     "pay/",
    #     include(("PayManager.urls", "PayManager"), namespace="PayManager"),
    #     name="支付接口",
    # ),
    path(
        "file/",
        include(("fileManager.urls", "fileManager"), namespace="fileManager"),
        name="文件接口",
    ),
    path(
        "rebot/",
        include(("rebot.urls", "rebot"), namespace="rebot"),
        name="AI接口",
    ),
    path(
        "wxpos/",
        include(("WxChatManager.urls", "WxChatManager"), namespace="WxChatManager"),
        name="AI接口",
    ),
    path(
        "tank/",
        include(("tankManager.urls", "tankManager"), namespace="tankManager"),
        name="Tank接口",
    ),
]

from .allRoute import get_all_url_dict
from rest_framework.compat import coreapi
from rest_framework.schemas.coreapi import SchemaGenerator, distribute_links


class CustomSchemaGenerator(SchemaGenerator):
    keys = get_all_url_dict(urlpatterns)

    def get_schema(self, request=None, public=False):
        self._initialise_endpoints()
        links = self.get_links(None if public else request)
        if not links:
            return None

        url = self.url
        if not url and request is not None:
            url = request.build_absolute_uri()

        distribute_links(links)
        links = dict(links)
        sess = links.copy()
        for k, v in sess.items():
            v = dict(v)
            for ck, cv in v.items():
                if self.keys.get(f"/{k}/{ck}/"):
                    links[k].update({self.keys.get(f"/{k}/{ck}/"): links[k].pop(ck)})
                else:
                    links[k].update({ck: links[k].pop(ck)})
        # print(links)
        return coreapi.Document(
            title=self.title, description=self.description, url=url, content=links
        )


if settings.DEBUG:
    from rest_framework.documentation import include_docs_urls

    schema_view = include_docs_urls(
        title="iClouds", description=DESCRIPTION, generator_class=CustomSchemaGenerator
    )
    urlpatterns.append(
        # 文档页面
        path("docs/", schema_view, name="接口文档"),
    )

    urlpatterns.append(
        re_path(
            "media/(?P<path>.*)",
            serve,
            {"document_root": settings.MEDIA_ROOT},
            name="media",
        ),
    )
